<?php
include('/var/www/html/new/com.inc.php');
include('/var/www/html/new/d/class/Member.php');
include('/var/www/html/new/d/class/abc/Member.class.php');
include("/var/www/html/new/d/class/abc/Product.class.php");
include('/var/www/html/new/d/class/abc/PFTCoreAPI.class.php');
if(!isset($_SESSION['sid'])) exit('{"status":"fail","msg":"登陆过期","code":0}');
if(!isset($_REQUEST['act'])) exit('{"status":"fail","msg":"参数错误"}');

$ac = $GLOBALS['_ac'];
$pw = $GLOBALS['_pw'];
$s  = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));

$memberSID = $_SESSION['sid'];
$m         = new Member($s, $memberSID);

switch(trim($_REQUEST['act'])){
	
	// 获取可销售的景区列表
	case 'lands':
		$salePros = $m->salePros(); // 2000 个产品大概 0.6 - 0.8 s
		$lids = array();// 景区模糊查询
		if(isset($_REQUEST['ltitle'])){
			$ltitle = $_REQUEST['ltitle'];
			$sql = "select id from uu_land where title like '%$ltitle%'";
			$GLOBALS['le']->query($sql);
			while($row=$GLOBALS['le']->fetch_assoc()) $lids[] = $row['id'];
		}
		
		$lids_count = count($lids);// 模糊查询出来的景区ID
		$sale_lands = array();
		foreach($salePros as $row){
			if(!isset($sale_lands[$row['lid']])) $sale_lands[$row['lid']] = array('id'=>$row['lid'],'title'=>$row['title']);
		}
		exit(json_encode(array('status'=>'success', 'lists'=>array_values($sale_lands))));
		break;
	// 根据景区获取可销售产品
	case 'prods':
		$prods    = array();
		$today    = date('Y-m-d');
		$landid   = $_REQUEST['lid'] + 0;
		$salePros = $m->salePros($landid); // 2000 个产品大概 0.6 - 0.8 s
		$suppliers = abc777\Member::Supplier($memberSID);
		$suppliers[$_SESSION['sid']] = $_SESSION['dname'];
		if($_SESSION['dtype']==6){
			$sql = "select dname from pft_member where id={$_SESSION['sid']} limit 1";
			$GLOBALS['le']->query($sql);
			$GLOBALS['le']->fetch_assoc();
			$suppliers[$_SESSION['sid']] = $GLOBALS['le']->f('dname');
		}
		
		// print_r($salePros);
		foreach($salePros as $row){
			if(!isset($suppliers[$row['sapply_did']])) continue;
			$ps = abc777\Product::Dynamic_Price_And_Storage($s, $_SESSION['saccount'], $row['pid'], $row['sapply_did'], $today);
			if($ps['js']['p']==-1) continue;
			$prods[] = array(
				'pid' => $row['pid'],
				'sapply_did' => $row['sapply_did'],
				'dname' => $suppliers[$row['sapply_did']],
				'ttitle' => $row['ttitle'],
				'ltitle' => $row['title'],
				'js' => $ps['js']['p'],
				'ls' => $ps['ls']['p'],
				'lid' => $row['lid'],
				'tid' => $row['tid'],
				'effective'=> _htmlValid($row),
				'delaydays' => $row['delaydays'],
				'delaytype' => $row['delaytype'],
				'order_start' => $row['order_start'],
				'order_end' => $row['order_end'],
				'fid'    => $memberSID,
			);
		}
		exit(json_encode(array('status'=>'success', 'lists'=>$prods, 'length'=>count($prods))));
		break;
	// 获取可销售产品列表
	case 'salePros':
		$salePros = $m->salePros(); // 2000 个产品大概 0.6 - 0.8 s
		$response = array();
		foreach($salePros as $row){
			$response[] = array(
				'aid'    => $row['sapply_did'], 
				'pid'    => $row['pid'], 
				'fid'    => $memberSID,
				'ttitle' => $row['ttitle'], 
				'ltitle' => $row['title'],
			);
		}
		exit(json_encode(array('status'=>'success', 'lists'=>$response, 'length'=>count($response))));break;
	
	// 主题刷选产品列表(home_16u.html)
	case 'theme_prod_list':

		$salePros = $m->salePros();

		// 渠道配置排序
		$channel_a = array();
		$sql = "select pid,px from pft_sale_channel where fid=$memberSID";
		$GLOBALS['le']->query($sql);
		while($row = $GLOBALS['le']->fetch_assoc()) $channel_a[$row['pid']] = $row['px'];
		foreach($salePros as $key=>$row){
			if(isset($channel_a[$row['pid']])) $salePros[$key]['px'] = $channel_a[$row['pid']];
		}
		
		uasort($salePros,  function($a,$b){
			$tuijiana = $a['px'];
			$tuijianb = $b['px'];
			if($tuijiana==$tuijianb) return 0;
			return ($tuijiana>$tuijianb) ? -1 : 1;
		});
		// print_r($salePros);
		$prods_a = array();
		$theme_a = array();
		$theme_s = $_REQUEST['theme'];
		$city    = isset($_REQUEST['city']) ? $_REQUEST['city']:'';
		foreach($salePros as $key=>$row){
			
			if($city){// 城市过滤
				if($row['area']=='') continue;
				$area_a = explode('|', $row['area']);
				if($area_a[1]!=$city) continue;
			}
			
				
				
			// 主题过滤
			if($theme_c>=8) break;
			if($row['topic']=='') continue;
			$topic_a = explode(',', $row['topic']);
			if(!in_array($theme_s, $topic_a)) continue;

			// 价格过滤
			$pStorage = PFTCoreAPI::pStorage($s, $_SESSION['saccount'], $row['pid'], $row['sapply_did'], date('Y-m-d'));
			if($pStorage['js']['p']==-1) continue;
			
			$imgpath = strpos($row['imgpath'], 'http')!==false ? $row['imgpath']: 'http://www.12301.cc/'.$row['imgpath'];
			
			if(!isset($prods_a[$row['lid']])){
				$prods_a[$row['lid']] = array(
					'ltitle'  => $row['title'],
					'imgpath' => $imgpath,
					'opentime'=> $row['runtime'],
				);
				$theme_c += 1;
			}
			
			$prods_a[$row['lid']]['ticket'][] = array(
				'pid'    => $row['pid'],         // 产品PID
				'aid'    => $row['sapply_did'],  // 供应商ID
				'ttitle' => $row['ttitle'],      // 门票名称
				'ls' => $pStorage['ls']['p'],    // 零售价
				'js' => $pStorage['js']['p'],    // 结算价
			);
		}
		$prods_a = array_values($prods_a);
		// print_r($prods_a);
		exit(json_encode(array('status'=>'success','list'=>$prods_a)));
	// 获取销售城市
	case 'sale_city':
		$response = array('citys'=>array());
		$salePros = PFTCoreAPI::salePros(array('fields'=>array('l.area')));
		if(count($salePros)){
			include '/var/www/html/new/d/common/area.inc.php';
			foreach($salePros as $row){
				if($row['area']=='') continue;
				$area_a = explode('|', $row['area']);
				if(isset($areas[$area_a[1]])) $response['citys'][$area_a[1]] = $areas[$area_a[1]];
			}
		}
		// IP城市定位
		// include '/var/www/html/new/d/class/AccessRecord.php';
		// $API = 'http://ip.taobao.com/service/getIpInfo.php?ip='.AccessRecord::remote_address();
		// $json_api = file_get_contents($API);
		// $json_api = json_decode($json_api, true);
		// if($json_api['code']==0){
			// $response['location'] = $json_api['data']['city'];
		// }else 
			$response['location'] = current($response['citys']);
		exit(json_encode($response));
	default:exit('{"status":"fail","msg":"参数错误"}');break;
}




function _htmlValid($p_info){
	if($p_info['delaydays']>0){
		if($p_info['delaytype']==0) $valid = '游玩日期(含)'.$p_info['delaydays'].'天内有效';
		if($p_info['delaytype']==1) $valid = '下单日期(含)'.$p_info['delaydays'].'天内有效';
	}
	if($p_info['order_start']=='' && $p_info['delaydays'] == 0) $valid = '订单当天有效';
	
	if($p_info['order_start']!='' && $p_info['order_end']!='' ){
		$valid = '有效日期从'.$p_info['order_start'].' 至 '.$p_info['order_end'];
	}elseif($p_info['order_start']=='' && $p_info['order_end']!=''){
		$valid = '截止'.$p_info['order_end'];
	}
	return $valid;
}

